#include<bits/stdc++.h>
using namespace std;

int min_time(string s1,int n){
    long long res=s1.size();
    long long no_res=s1.size();
    // 不使用魔法
    for(int i=1;i<s1.size();++i){
        int len=abs(s1[i]-'0'-(s1[i-1]-'0'));
        if(len!=25){
            no_res = no_res + len;
        }
        else{
            ++no_res;
        }
    }
        
    res = no_res;
    for(int i=0;i<s1.size();++i){
        long long yes_res=s1.size();
        for(int j=1;j<=i;++j){
            int len=abs(s1[j]-'0'-(s1[j-1]-'0'));
            if(len!=25){
                yes_res = yes_res + len;
            }
            else{
                ++yes_res;
            }
        }
        yes_res = yes_res+n;
        for(int j=i+n+1;j<s1.size();++j){
            int len=abs(s1[j]-'0'-(s1[j-1]-'0'));
            if(len!=25){
                yes_res = yes_res + len;
            }
            else{
                ++yes_res;
            }
        }
        res = min(res,yes_res);
    }
    // cout<<res<<endl;
    return res;
}

int main(int argc, char const *argv[]){
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s1;
    int n;
    // cout<<min_time("ECAAAAAA",2)<<endl;
    while(cin>>s1>>n){
        int res = INT_MAX;
        for(int i=0;i<=s1.size();++i){
            string s2=s1.substr(0,i);
            string s3=s1.substr(i,s1.size()-i);
            string s2_r="";
            for(int j=0;j<s2.size();++j){
                s2_r = s2[j] + s2_r;
            }
            string s3_r="";
            for(int j=0;j<s3.size();++j){
                s3_r = s3[j] + s3_r;
            }
            // cout<<s2_r+s3<<endl;
            // cout<<s3+s2<<endl;
            res = min(res,min_time(s2+s3,n));
            res = min(res,min_time(s2+s3_r,n));
            res = min(res,min_time(s2_r+s3_r,n));
            res = min(res,min_time(s2_r+s3,n));
            res = min(res,min_time(s3+s2,n));
            res = min(res,min_time(s3+s2_r,n));
            res = min(res,min_time(s3_r+s2,n));
            res = min(res,min_time(s3_r+s2_r,n));
            
            // res = min(res,min_time(s3+s2,n));
        }
        // int res = min_time(s1,n);
        cout<<res<<endl;
    }
    return 0;
}
